﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LINQ_Experiments
{
    class Program
    {
        static void Main(string[] args)
        {
            //InsertCustomer();
            //UpdateCustomer();
            //DeleteCustomer();
            //ShowAllCustomers();
            //ShowAllLondonCustomers();
            DisplayCustomeOrder();
            
        }

        public static void InsertCustomer()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            
            var customer = new Customer() 
                { CustomerID = "CHIPS", CompanyName = "Mr. Chips" };
            db.Customers.InsertOnSubmit(customer); 
            
            
            db.SubmitChanges();
        }

        public static void UpdateCustomer()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            
            var only = db.Customers.SingleOrDefault(c => c.CustomerID == "CHIPS");
            only.CompanyName = only.CompanyName + "1";
            
            db.SubmitChanges();
        }

        public static void DeleteCustomer()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var only = db.Customers.SingleOrDefault(c => c.CustomerID == "CHIPS");
            db.Customers.DeleteOnSubmit(only);

            db.SubmitChanges();
        }

        public static void ShowCustomersByCity()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var customers = from c in db.Customers
                            where c.City == "London"
                            select c;

            foreach (Customer c in customers)
            {

                Console.WriteLine(c.CompanyName);
            }
        }
        public static void ShowAllCustomers()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            var customers = db.Customers;
            //var customers = from c in db.Customers
            //                select c;

            foreach (Customer c in customers)
            {

                Console.WriteLine(c.CompanyName);
            }
        }

        //public static void ShowAllLondonCustomers()
        //{
        //    DataClasses1DataContext db = new DataClasses1DataContext();
            
        //    //var customers = LondonCustomers;
        //    var customers = from c in db.GetTable<Customer>().OfType<LondonCustomers>()
        //                    select c;

        //    foreach (Customer c in customers)
        //    {

        //        Console.WriteLine(c.CompanyName);
        //    }
        //}

        public static void DisplayCustomeOrder()
        {


            using (DataClasses1DataContext nw = new DataClasses1DataContext())
            {

                var q = from cus in nw.Customers

                        where cus.City == "London"

                        select cus;

                
                // Loop through customers and print out orders since January 1, 1998

                foreach (Customer customer in q)
                {

                    Console.WriteLine(customer.CompanyName + ": ");

                    // Note line below to explicitly load all orders for each customer

                    ////customer.Orders.Load(); //no need to call it, as data are already been loaded
                    ////but a seperate query will be executed for each customer order
                    //foreach (Order order in customer.Orders.Where(o => o.OrderDate > new DateTime(1998, 1, 1)))
                    //{

                    //    Console.WriteLine("\t{0},{1}", order.OrderID, order.OrderDate);

                    //}

                }

                Console.ReadLine();
                //In the above example, a separate query is executed to retrieve the Orders for each Customer. If it is known in advance that we need to retrieve the orders for all customers, we can use LoadOptions to request that the associated Orders be retrieved along with the Customers, in a single request. 

            }
        }
    }

    public partial class Customer
    {

        partial void OnPhoneChanging(string value)
        {

            Regex phoneNum = new Regex(@"^[2-9]\d{2}-\d{3}-\d{4}$");

            if (phoneNum.IsMatch(value) == false)

                throw new Exception("Please enter a valid Phone Number");

        }

    }
}
